#!/bin/bash


HARBOR_HOME=${DEFAULT_HARBOR_HOME:-'/opt/software/harbor'}
mkdir -p $HARBOR_HOME/data
mkdir -p $HARBOR_HOME/certs


# 生成https签名
cd $HARBOR_HOME/certs || exit 1
# 生成CA根证书（相当于CA机构）
# 私钥
openssl genrsa -out ca.key 4096
# 证书
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Chongqing/L=Chongqing/O=newtouch/OU=yfb/CN=harbor-cq.newtouch.io/emailAddress=378661553@qq.com" \
-key ca.key -out ca.crt

# 生成服务器证书（相当于服务器拿私钥以及CA根证书签名后去获得服务器的https证书）
# 私钥
openssl genrsa -out server.key 4096
# 证书签名请求
openssl req -new -sha512 \
-subj "/C=CN/ST=Chongqing/L=Chongqing/O=newtouch/OU=yfb/CN=harbor-cq.newtouch.io/emailAddress=378661553@qq.com" \
-key server.key \
-out server.csr
# 生成多个域名请求
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1=harbor.zfsw.io
EOF

# 生成harbor仓库主机的证书
openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in server.csr \
-out server.crt

# 最后修改 harbor.yml 中的配置文件，将生成的服务器证书配置进去
#  certificate: /usr/local/harbor/certs/server.crt
#  private_key: /usr/local/harbor/certs/server.key


# Harbor 官网 https://goharbor.io/
#wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz

cd $HARBOR_HOME || exit
has_harbor_file=$(ls -l | grep harbor-offline-installer-v2.5.0.tgz)
if [ "$has_harbor_file" = "0" ]; then
    echo 'No installation package found (harbor-offline-installer-v2.5.0.tgz), please download the installation package first'
    exit 1
fi
tar -xzvf harbor-offline-installer-v2.5.0.tgz
cd harbor
cp harbor.yml.tmpl harbor.yml

# 自行更改为服务器内部ip
# hostname
# http
# https
# data_volume
echo '请修改 harbor.yml 中的 "hostname: <主机IP>" 配置，最后执行 ./install.sh'
exit 0

#安装
./install.sh
systemctl daemon-reload && systemctl restart docker
docker login --username=admin --password=Harbor12345 harbor.zfsw.io:31443
